//
//  StatusOriginalView.swift
//  上海微博
//
//  Created by co on 16/3/2.
//  Copyright © 2016年 王南侠. All rights reserved.
//

import UIKit
import SnapKit

/// TODO : 微博头像尺寸
private let CZStatusIconSize = CGSize(width: 36, height: 36)


/// 原创微博 - cell的子视图
class StatusOriginalView: UIView {
    
    /// 微博视图模型
    var viewModle:StatusViewModle? {
        didSet{
            // 设置数据
//            print(viewModle?.status.user)
            // 设置圆角头像(需要扩展参数)
            iconView.cz_setImageWithURL(viewModle?.status.user?.profile_image_url, placeholderName: "avatar_default_big",
                size:CZStatusIconSize,isCorner: true
            )
            // 姓名
            nameLable.text = viewModle?.status.user?.screen_name
            
            // 设置图标
            memberImageView.image = viewModle?.userMemberImage
            vipIconView.image = viewModle?.useVipImage

            // TODO: -

            print(viewModle?.status.created_at)
            sourceLable.text = viewModle?.sourceText
            timeLable.text = viewModle?.creatTime?.cz_dateDescription//千万不要手打代码
            
            contentLable.text = viewModle?.status.text
            
             // 更新 配图视图大小
            let offset = viewModle?.status.pic_urls?.count > 0 ? CZStatusCellLayout.margin : 0
            picturesView.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(viewModle!.picturesViewSize)
                make.top.equalTo(contentLable.snp_bottom).offset(offset)
            }
            // 设置配图 视图内容
            picturesView.pic_urls =  viewModle?.status.pic_urls
            
            // 更新视图大小 注意不要和上面的代码条换位置
            picturesView.updateImageSize(viewModle!.picturesViewSize)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 私有控件
    ///头像图标
    private let iconView:UIImageView = UIImageView(cz_imageName: "avatar_default_big")
    /// 姓名标签
    private let nameLable:UILabel = UILabel(cz_text: "灿灿")
    /// 会员图标
    private let memberImageView:UIImageView = UIImageView(cz_imageName: "common_icon_membership_level1")
    /// VIP 图标
    private let vipIconView:UIImageView = UIImageView(cz_imageName: "avatar_vip")
    /// 时间
    private let timeLable:UILabel = UILabel(cz_text: "创建时间", fountSize: 10, color: ZCAppearanceTintColor)
    /// 来源
    private let sourceLable = UILabel(cz_text: "来源", fountSize: 10)
    /// 内容视图
    private let contentLable:UILabel = UILabel(cz_text: "灿灿微博", fountSize: 15)
    /// 配图视图
    private let picturesView:StatusPictureView = StatusPictureView()
    
}


// MARK: - 设置界面
extension StatusOriginalView {
    
    
    private func setupUI(){
        
        // 0.设置背景颜色
        backgroundColor = UIColor.whiteColor()
        
        // 1.添加控件
        addSubview(iconView)
        addSubview(nameLable)
        addSubview(memberImageView)
        addSubview(vipIconView)
        addSubview(timeLable)
        addSubview(sourceLable )
        addSubview(contentLable)
        addSubview(picturesView)
        
        let margin:CGFloat = 12.0
        
        // 测试代码
        let count = rand() % 20 + 1
        var str = ""
        for _ in 0..<count{
            str += "新浪微博"
        }
    
        contentLable.text = str
        // 测试头像
        UIImage(named: "avatar_default_big")?.cz_asynDrawImage(CZStatusIconSize, isCorner: true,  finshed: { (image) -> () in
            self.iconView.image = image
        })
        // 3自动布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(margin)
            make.left.equalTo(margin)
            make.size.equalTo(CZStatusIconSize)
        }
        
        nameLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
        }
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLable)
            make.left.equalTo(nameLable.snp_right).offset(margin)
            
        }
        vipIconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_right).offset(-4)
            make.centerY.equalTo(iconView.snp_bottom).offset(-4)
        }
        timeLable.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
        }
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLable)
            make.left.equalTo(timeLable.snp_right).offset(margin)
        }
        contentLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(margin)
            
            //文字换行
            make.right.equalTo(self).offset(-margin)
            
        }
        picturesView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentLable)
            make.top.equalTo(contentLable.snp_bottom).offset(margin)
            
            // 默认不设置配图大小
            make.size.equalTo(CGSizeZero)
            
            // 文字的高度 设置底部约束，能够自动计算高度 ！很重要！！
            make.bottom.equalTo(self).offset(-margin)
        }
        

    }
}